# -*- coding: utf-8 -*-

import time

from . import JiangHu

class Idle(JiangHu.JiangHu):
    def __init__(self, thread_id, name, index, public_data,ignore_block):

        super().__init__(thread_id, name, index, public_data,ignore_block)

    def first_init(self):
        self.publicData["游戏线程大漠插件是否绑定"] = 0
        self.publicData["人物不在黄陵次数"] = 0
        if self.publicData["系统"]["次要阶段"] == 0 or self.publicData["系统"]["次要阶段"] == "":
            self.publicData["系统"]["次要阶段"] = 1
        # self.publicData["奇遇选择配置"] = self.dm.GetDirPath(r"pic\奇遇")
        self.data["是否进船坞扶桑"]=False
        self.data["是否进入论剑"] = False
        self.data["是否论剑中"] = False
        self.data["是否进入黄陵"] = False
        self.data["是否黄陵中"] = False
        self.data["是否商人购买"] = False
        self.data["是否进入龙"] = False # todo


    # def run(self):
    #     self.log.info("游戏线程启动")
    #     self.log_stage()
    #     self.first_init()
    #     while True:
    #         self.log.debug("开始查找创窗口" + self.get_main_stage())
    #         dota2_hwnd = self.dm.FindWindow(self.publicData["dota2_class_name"], self.publicData["dota2_tittle"])
    #         self.log.debug("当前游戏阶段" + self.get_main_stage())
    #         self.log.debug("巡检是否绑定 %s" % self.publicData["系统数据"]["巡检是否绑定"])
    #         self.log.info("dota2_hwnd %s " % dota2_hwnd)
    #         if self.publicData["系统数据"]["巡检是否绑定"] and dota2_hwnd > 0:
    #             self.hwnd = dota2_hwnd
    #             res = self.BindDOTA2Game()
    #             if res == 1:
    #                 break
    #         elif self.publicData["系统数据"]["巡检是否绑定"] == False or dota2_hwnd == 0:
    #             time.sleep(3)
    #             continue


    def Process(self):
        if self.get_main_stage() == "":

            res = self.dm.SearchPngClick(810, 425, 1245, 655, "dota2\离开比赛.bmp|dota2\离开比赛2.bmp", 0.8, 1, 1,
                                         1, 10, 10)
            if res == 1:
                self.log.info("重新启动后 点击上把离开比赛")
            self.retryInRoomList()

            self.modifyStage(main_stage="查找房间")

        self.log.info("当前阶段 %s" % self.publicData["系统"]["主要阶段"])
        main_stage = self.publicData["系统"]["主要阶段"]

        if main_stage == "查找房间":
            is_get = self.dm.SearchPngClick(810, 425, 1245, 655, "dota2\离开比赛.bmp|dota2\离开比赛2.bmp", 0.8, 1,
                                            1, 1, 10, 10)
            if is_get == 1:
                self.log.info("结束上一把比赛 离开游戏")
            is_get1 = self.dm.SearchPngClick(1030, 50, 1100, 720, "dota2\房间.bmp", 0.8, 1, 0.01, 0, 0, 0)
            is_get2 = self.dm.SearchPngClick(500, 325, 750, 375, "dota2\接受开始.bmp", 0.8, 1, 0.5, 0, 0, 0)
            if is_get1 == 1 or is_get2 == 1:
                self.modifyStage(main_stage="进入房间")
            else:
                self.dm.LeftClick(1097, 118, 1)
                for i in range(10):
                    is_get = self.dm.SearchPngClick(50, 125, 350, 175, "dota2\房间前感叹号.bmp", 0.9, 1, 1, 2, 300, 3)
                    if is_get == 1:
                        break
                    else:
                        self.log.info("点击刷新按钮")
                        self.dm.LeftClick(1097, 118, 1)
        elif main_stage == "进入房间":
            is_get = self.dm.SearchPngClick(500, 325, 750, 375, "dota2\接受开始.bmp|dota2\接受开始2.bmp", 0.8, 1,
                                            0.5,
                                            1,
                                            100, 15)
            if is_get == 1:
                self.publicData["系统数据"]["总局数"] += 1
                # 点击接受后有时不生效
                is_get = self.dm.SearchPngClick(500, 325, 750, 375, "dota2\接受开始.bmp|dota2\接受开始2.bmp", 0.8,
                                                5, 3,
                                                1, 100, 15)
                if is_get == 1:
                    self.dm.SearchPngClick(500, 325, 750, 375, "dota2\接受开始.bmp|dota2\接受开始2.bmp", 0.8, 5, 3,
                                           1,
                                           100, 15)
                self.modifyStage(main_stage="加载游戏")
        elif main_stage == "加载游戏":
            self.modifyStage(second_stage=1)
            if self.data["开始跑马时间"] < 150.0:
                is_get1 = self.dm.WaitPngDisappear(590, 510, 700, 600, "存档\拼命加载中.bmp", 0.8, 1, 0)
                is_get2 = self.dm.WaitPngDisappear(0, 0, 30, 30, "存档\跑马左上角返回.bmp", 0.8, 1, 0)
                if is_get1 == 1 and is_get2 == 1:
                    self.data["开始跑马时间"] = time.time()
            if time.time() - self.data["开始跑马时间"] > self.publicData["系统"]["跑马超时时间"] and self.data["开始跑马时间"] > 150.0:
                self.log.info("启动跑马重连机制")
                self.dm.LeftClick(16, 16, 1)
                self.data["开始跑马时间"] = 0.0
                # 可能会有多余的断开链接弹窗
                is_get = self.dm.SearchPngClick(450, 250, 900, 500,
                                                "dota2\已断开服务器连接.bmp|dota2\已断开服务器连接1.bmp|dota2\已断开服务器连接2.bmp|dota2\已断开服务器连接3.bmp|dota2\已断开服务器连接4.bmp|dota2\已断开服务器连接5.bmp",
                                                0.8, 1, 0, 1, 120, 90)
                if is_get == 1:
                    time.sleep(1)
                self.dm.SearchPngClick(1010, 625, 1245, 655, "dota2\断开连接.bmp|""dota2\断开连接2.bmp", 0.8, 10, 0,
                                       1, 10, 10, is_wait=1)
                return  0

            is_get = self.dm.SearchPngClick(1000, 625, 1250, 700, "开始游戏.bmp", 0.8, 5, 0.5, 0, 0, 0, is_wait=1)

            if is_get == 1 :
                self.modifyStage(main_stage="人物选择")
                self.log.info("混子人物选择")
            is_get2 = self.dm.SearchPngClick(500, 1, 650, 35, "存档\选难度界面倒计时框框.bmp", 0.95, 1, 0.5, 0, 0, 0)
            if is_get2==1:
                self.data["开始跑马时间"] = 0.0

        elif main_stage == "人物选择":
            self.ChooseSect()
        elif main_stage == "分解":
            self.publicData["系统数据"]["进入游戏数"] += 1
            self.data["本局开始时间"] = time.time()

            is_get = self.dm.SearchPngClick(125, 650, 165, 675, "f4.bmp", 0.8, 180, 1, 0, 0, 0)
            time.sleep(10)
            self.log.info("开始保存大哥头像")
            dm_ret = self.dm.Capture(42, 50, 49, 57, "dage.bmp")
            self.log.info("保存大哥头像结束 结果 %s" % dm_ret)
            time.sleep(1)
            self.log.info("开始分解装备 吃静脉奇石")
            self.EquipArms()

            if self.publicData["每日"]["是否轮回狂风快刀"]:
                if self.index % self.publicData["每日"]["轮回狂风间隔"] == 0:
                    self.Reincarnation()
            if self.publicData["每日"]["金锭"]:
                self.CollectGoldIngots()
            self.EatStone()
            self.modifyStage(main_stage="发育")
        elif main_stage == "发育":

            res=self.F3SaveLife()
            if res==1:
                self.modifyStage(main_stage="等待")
                self.BotSay()
                self.modifyStage(second_stage=99999)
        elif main_stage == "等待":
            self.Challenge_Archive()
            res = self.dm.SearchPngClick(111, 672, 129, 690,r"主线\1号位图标.bmp", 0.90, 1, 0, 0, 0, 0)
            self.log.info("龙进入情况%s" % res)
            time.sleep(3)
        elif main_stage == "存档":
            second_stage = self.get_second_stage()
            self.log.info(second_stage)
            res = self.dm.SearchPngClick(111, 672, 129, 690, r"主线\1号位图标.bmp", 0.90, 1, 0, 0, 0, 0)
            self.log.info("龙进入情况%s" % res)
            if second_stage == "存档":
                for i in range(10):
                    res = self.dm.SearchPngClick(111, 672, 129, 690, r"主线\1号位图标.bmp", 0.90, 1, 0, 0, 0, 0)
                    self.log.info("龙进入情况%s" % res)
                    res=self.CheckMode()
                    self.log.info("当前模式: %s"%res)
                    self.publicData["基础配置"]["模式"] = res
                    if self.publicData["存档"]["是否打龙"] is False:
                        self.data["是否进入龙"] = True
                    if res=="":
                        continue
                    elif res=="侠客行":
                        self.data["是否进入龙"] = True
                        break
                    elif res == "异国志":
                        if self.publicData["存档"]["是否进扶桑"] is False:
                            self.data["是否进船坞扶桑"]=True
                        break
                    elif res == "华山之巅":
                        self.data["是否进入黄陵"] = True
                        break
                # 区别第一次进入
                self.publicData["系统数据"]["防守成功数"] += 1
                if self.publicData["存档"]["是否刷黄陵"] is False:
                    self.data["是否进入黄陵"] = True
                if self.publicData["存档"]["是否论剑"] is False:
                    self.data["是否进入论剑"] = True
                self.modifyStage(second_stage="")
            if not self.data["是否黄陵中"]:
                self.log.info("不在黄陵中使用背包所有物品")
                if self.publicData["基础配置"]["模式"] in ["普通", "y", "侠客行", "华山之巅"]:
                    self.UseAllBossSaveData()
                else:
                    self.UseAllItems()
            self.log.info( self.data["是否论剑中"])
            if self.data["是否论剑中"] is False and self.data["是否黄陵中"] is False :
                self.F3Save()
                # 辅助不需要看大哥 顺序不定
                if self.data["是否商人购买"] is False:
                    self.log.info("商人购买")
                    self.BuyArchiveMerchant()
                    self.data["是否商人购买"]=True

                if self.data["是否进入龙"] == 0:
                    self.log.info("检查进龙")
                    res = self.dm.SearchPngClick(111, 672, 129, 690, r"主线\1号位图标.bmp", 0.90, 5, 0.3, 0, 0, 0)
                    self.log.info("龙进入情况%s" % res)
                    if res ==1:
                        res=self.JoinDragon()
                        if res==1:
                            self.data["是否进入龙"] = 1
                if self.data["是否进船坞扶桑"] is False:
                    self.log.info("检查进船坞扶桑")
                    res=self.JoinDockAndFusang()
                    if res ==1:
                        self.data["是否进船坞扶桑"] = True

                if self.data["是否进入论剑"] is False and self.data["是否进船坞扶桑"] is True and self.publicData["存档"]["是否论剑"] is True:
                    self.log.info("检查进论剑")
                    res = self.CompetitionOnHuashan()
                    self.log.info(res)
                    if res == 0:
                        self.data["是否进入论剑"] = True
                        self.data["是否论剑中"]=True
                    elif res == 1:
                        self.data["是否论剑中"] = False

                if self.data["是否进入黄陵"] is False and self.data["是否进船坞扶桑"] is True:
                    res = self.ImperialMausoleumEnter()
                    if res == 1:
                        self.data["是否进入黄陵"] = True
                        self.data["是否黄陵中"] = True
                        self.data["是否论剑中"] = False
            elif self.data["是否论剑中"] and self.publicData["存档"]["是否论剑"] is True:
                self.log.info("检查大哥是否开启黄陵")
                res = self.ImperialMausoleumEnter()
                if res == 1:
                    self.data["是否进入黄陵"] = True
                    self.data["是否黄陵中"] = True
                    self.data["是否论剑中"] = False
                res = self.CompetitionOnHuashan()
                if res == 0:
                    self.data["是否进入论剑"] = True
                    self.data["是否论剑中"] = True
                elif res == 1:
                    self.data["是否论剑中"] = False
            elif self.data["是否黄陵中"]:
                self.ImperialMausoleuChose()
                self.log.error("检查是否在黄陵")
                is_get = self.CheckPersonPosition(93, 649, 0.8, 1, 1, 100, 59, 620, 480)
                if is_get == 1:
                    self.publicData["人物不在黄陵次数"] = 0
                else:
                    self.log.info("人物不在黄陵次数")
                    self.publicData["人物不在黄陵次数"] += 1
                if self.publicData["人物不在黄陵次数"] > 8:
                    self.modifyStage(main_stage="存档等待")



        elif main_stage == "存档等待":
            for i in range(60):
                self.log.info(i)
                if self.publicData["基础配置"]["模式"] in ["普通", "y", "侠客行", "华山之巅"]:
                    self.UseAllBossSaveData()
                    self.UseAllBossSaveData()
                else:
                    self.UseAllItems()
                    self.UseAllItems()
                if self.publicData["每日"]["排行榜奖励"]:
                    self.GetRankingReward()
                self.log.info(self.publicData["辅助"]["提前退出"])
                if self.publicData["辅助"]["提前退出"]:
                    break
                self.log.info(self.publicData["大哥不在线次数"])
                if self.publicData["大哥不在线次数"] >1:
                    break
                time.sleep(1)
            self.modifyStage(main_stage="退出游戏")
        elif main_stage == "退出游戏":

            self.log.info("本局游戏结束")
            self.dm.UnBindWindow()
            time.sleep(1)

            return 1
        else:
            self.log_stage()
    def BotSay(self):
        x = "I am a robot, if I affect you, please change your password and kick me out."
        self.dm.KeyPressChar("enter")
        for i in x:
            if i == " ":
                self.dm.KeyPressChar("space", step=0.01)
            else:
                self.dm.KeyPressChar(i, step=0.01)
        self.dm.KeyPressChar("enter")

    def F3SaveLife(self):
        flag=False
        for i in range(10):
            self.GoHome(100,step=0.1)
            time.sleep(1)
            # 检查是否在家
            is_in = self.CheckPersonPosition(68, 660, 0.8, 1, 1, 100, 100, 980, 620)
            if is_in == 1:
                is_in = self.CheckPersonPosition(68, 660, 0.8, 1, 1, 100, 100, 980, 620)
                if is_in == 1:
                    flag = True
                    break
        if flag:
            for i in range(3):
                is_get = self.DotaCheckMoveAttack(77, 670,750, 423)
                if is_get==1:
                    return 1
        return 0

    def F3Save(self):
        for i in range(2):
            self.GoHome(25,step=0.1)
            time.sleep(1)
            # 检查是否在家
            is_in = self.CheckPersonPosition(68, 660, 0.8, 1, 1, 100, 100, 980, 620)
            if is_in == 1:
                is_in = self.CheckPersonPosition(68, 660, 0.8, 1, 1, 100, 100, 980, 620)
                if is_in == 1:
                    return 1

        return 0


    def UnlockArms(self):
        self.log.info("开始解锁武器")
        is_get=self.AccessNPC(62, 663,581, 222)
        path = "存档\大旗秘藏.bmp|存档\见禅知意.bmp|存档\浪迹天涯.bmp" # todo
        path = "存档\大旗秘藏.bmp|存档\见禅知意.bmp"
        if is_get==1:
            is_get = self.dm.WaitPngDisappear(447, 619, 925, 714, path, 0.9, 5, 0.102, 2, 10, 10,is_wait=1)
            if is_get==0:
                return 1
        return 0

    # 选择w技能
    def ChooseWKill(self,name1,name2):
        self.log.info("刷w功法")
        self.GoSect()
        path =r"功法\%s.bmp|功法\%s.bmp"%(name1,name2)
        for i in range(10):
            self.dm.LeftClick(485, 634, 1)
            time.sleep(0.2)
            is_get = self.dm.SearchPngClick(456, 629, 495, 666, path, 0.8, 1, 0.1, 0, 0, 0)
            if is_get==1:
                return 1
        return 0

    # 拿洗髓经
    def GetMarrowCleansingSutra(self):
        for i in range(5):
            is_get = self.dm.SearchPngClick( 405, 330, 885, 475,"主线\洗髓经_极品.bmp|主线\洗髓经_极品2.bmp|主线\洗髓经_普通.bmp|主线\洗髓经_普通2.bmp|主线\洗髓经_垃圾.bmp|主线\洗髓经_垃圾2.bmp", 0.9, 20, 0.1, 0, 0, 0)
            if is_get==1:
                for i in range(1):
                    is_get = self.dm.SearchPngClick( 405, 330, 885, 475,"主线\洗髓经_极品.bmp|主线\洗髓经_极品2.bmp", 0.9, 1, 0, 1, 5, 5)
                    if is_get==1:
                        break
                    is_get = self.dm.SearchPngClick( 405, 330, 885, 475,"主线\洗髓经_普通.bmp|主线\洗髓经_普通2.bmp", 0.9, 1, 0, 1, 5, 5)
                    if is_get == 1:
                        break
                    self.dm.LeftClick(550, 400, 1)
                    break

                time.sleep(0.5)
                is_get = self.dm.SearchPngClick(530, 300, 800, 430, "主线\获取洗髓经.bmp", 0.9, 10, 0.5, 1, 20, 90,is_wait=1)
                if is_get==1:
                    break

    # 洗髓经全红
    def RedMarrowCleansingSutra(self):
        self.log.info("洗髓经天胡")
        time.sleep(0.1)
        path = "存档\一键天胡.bmp"
        self.dm.KeyPressChar("f1",num=2)
        time.sleep(0.2)
        self.dm.RightClick(781, 653, 1)
        is_get = self.dm.SearchPngClick(728, 600, 950, 720, path, 0.8, 5, 0.3, 1, 20, 8)
        self.dm.RightClick(781, 687, 1)
        is_get = self.dm.SearchPngClick(728, 600, 950, 720, path, 0.8, 5, 0.3, 1, 20, 8)
        self.dm.RightClick(817, 653, 1)
        is_get = self.dm.SearchPngClick(728, 600, 950, 720, path, 0.8, 5, 0.3, 1, 20, 8)
        self.dm.RightClick(817, 687, 1)
        is_get = self.dm.SearchPngClick(728, 600, 950, 720, path, 0.8, 5, 0.3, 1, 20, 8)
        self.dm.RightClick(750, 687, 1)
        is_get = self.dm.SearchPngClick(728, 600, 950, 720, path, 0.8, 5, 0.3, 1, 20, 8)



    # 升级洗髓经 todo 判断当前等级  怪物是否打死
    def UpgradeMarrowCleansingSutra(self):
        self.GoSect()
        self.dm.KeyPressChar("f1", num=2)
        path="存档\洗髓经1.bmp|存档\洗髓经2.bmp|存档\洗髓经3.bmp|存档\洗髓经4.bmp"
        path=self.dm.GetPicAbsPath(path)
        is_get = self.dm.SearchPngClick(732, 635, 746, 649, "存档\洗髓经z.bmp", 0.8, 1, 0.1, 0, 0, 0)
        is_get2 = self.dm.SearchPngClick(732, 635, 746, 649, "存档\空装备栏.bmp", 0.8, 1, 0, 0, 0, 0)
        if is_get==1 and is_get2==0:
            x,y,res=self.dm.FindPic(732, 635, 800, 700,path,"000000",0.8)
            if res==-1:
                self.dm.KeyPressChar("z")
                for i in range(10):
                    is_get = self.dm.SearchPngClick(500, 300, 781, 348, "主线\融合洗髓经.bmp|主线\融合洗髓经_遮挡.bmp", 0.8, 1, 0.25, 1, 41, 100)
                    if is_get == 1:
                        return 1
                    x, y, res = self.dm.FindPic(732, 635, 800, 700, path,"000000", 0.8)
                    if res !=-1:
                        break

        return 0



